﻿#ifndef ADDCELLL_H
#define ADDCELLL_H
#include <QUndoCommand>
#include "JistTableView.h"

class AddRemoveCell : public QUndoCommand
{
public:
    AddRemoveCell(JistDocument *document, int index, JistTableView *tableView, JistCommonData *data = NULL);

    ~AddRemoveCell();

protected:
    QVector< QList < JistCommonData > > m_AniData;
    QList < JistCommonData> m_SceneData;
    JistCommonData *m_OwnData;
    void removeSprite();
    void addSprite();
    void addCell();
    void removeCell();
    void addShadowCell();
    void addCellList();
    void removeCellList();
    void copyAnimation();
    void removeAnimation();

private:
    JistDocument *m_pDocument;
    JistTableView *m_pTableView;
    int m_Index;
    QModelIndexList m_ModelIndexList;
};

class AddSprite : public AddRemoveCell
{
public:

    AddSprite(JistDocument *document, int index, JistTableView *tableView, JistCommonData *data = NULL)
        : AddRemoveCell(document, index, tableView, data)
    {
        setText(QCoreApplication::translate("Undo Commands", "Add Sprite(s)"));
    }

    void undo()
    { removeSprite(); }

    void redo()
    { addSprite(); }

};


class RemoveSprite : public AddRemoveCell
{
public:
    /**
     * Creates an undo command that removes the layer at \a index.
     */
    RemoveSprite(JistDocument *document, int index, JistTableView *tableView)
        : AddRemoveCell(document, index, tableView)
    {
        setText(QCoreApplication::translate("Undo Commands", "Remove Sprite(s)"));
    }

    void undo()
    { addSprite(); }

    void redo()
    { removeSprite(); }
};



class AddCell : public AddRemoveCell
{
public:

    AddCell(JistDocument *document, int index, JistTableView *tableView, JistCommonData *data = NULL)
        : AddRemoveCell(document, index, tableView, data)
    {
        setText(QCoreApplication::translate("Undo Commands", "Add Cell(s)"));
    }

    void undo()
    { removeCell(); }

    void redo()
    { addCell(); }

};


class RemoveCell : public AddRemoveCell
{
public:
    /**
     * Creates an undo command that removes the layer at \a index.
     */
    RemoveCell(JistDocument *document, int index, JistTableView *tableView)
        : AddRemoveCell(document, index, tableView)
    {
        setText(QCoreApplication::translate("Undo Commands", "Remove Cell(s)"));
    }

    void undo()
    { addCell(); }

    void redo()
    { removeCell(); }
};

class AddShadowCell : public AddRemoveCell
{
public:

    AddShadowCell(JistDocument *document, int index, JistTableView *tableView, JistCommonData *data = NULL)
        : AddRemoveCell(document, index, tableView, data)
    {
        setText(QCoreApplication::translate("Undo Commands", "Add ShadowCell(s)"));
    }

    void undo()
    {  }

    void redo()
    { addShadowCell(); }

};


class AddCellList : public AddRemoveCell
{
public:

    AddCellList(JistDocument *document, int index, JistTableView *tableView,  QList < JistCommonData> *sceneData, JistCommonData *data)
        : AddRemoveCell(document, index, tableView)
    {
        setText(QCoreApplication::translate("Undo Commands", "Add Scene(s)"));
        if(!m_OwnData)
            m_OwnData = new JistCommonData();
        m_OwnData->copyData(*data);
        m_SceneData = *sceneData;
        for(int k = 0; k < m_SceneData.count(); k++){
            m_SceneData[k].copyData(sceneData->at(k));

//            ImageItem *src = m_SceneData[k].m_pIItem;
//            m_SceneData[k].m_FlipH = m_SceneData.value(k).m_FlipH;
//            m_SceneData[k].m_FlipV = m_SceneData.value(k).m_FlipV;

//            m_SceneData[k].m_pIItem = new ImageItem(&m_SceneData.value(k).m_img);
//            m_SceneData[k].m_pIItem->setZValue(src->zValue());
//            m_SceneData[k].m_pIItem->setPos(src->pos());
//            m_SceneData[k].m_pIItem->setOpacity(src->opacity());
//            m_SceneData[k].m_pIItem->setRotation(src->rotation());
//            m_SceneData[k].m_pIItem->setScale(src->scale());
        }
    }

    void undo()
    { removeCellList(); }

    void redo()
    { addCellList(); }

};


class RemoveCellList : public AddRemoveCell
{
public:
    RemoveCellList(JistDocument *document, int index, JistTableView *tableView,  QList < JistCommonData> *sceneData, JistCommonData *data)
        : AddRemoveCell(document, index, tableView)
    {
        setText(QCoreApplication::translate("Undo Commands", "Remove Scene(s)"));
        //sceneData->resize(sceneData->count() + 1);
        m_SceneData = *sceneData;
        for(int k = 0; k < m_SceneData.count(); k++){

            m_SceneData[k].copyData(sceneData->at(k));
//            ImageItem *src = m_SceneData[k].m_pIItem;
//            m_SceneData[k].m_FlipH = m_SceneData.value(k).m_FlipH;
//            m_SceneData[k].m_FlipV = m_SceneData.value(k).m_FlipV;

//            m_SceneData[k].m_pIItem = new ImageItem(&m_SceneData.value(k).m_img);
//            m_SceneData[k].m_pIItem->setZValue(src->zValue());
//            m_SceneData[k].m_pIItem->setPos(src->pos());
//            m_SceneData[k].m_pIItem->setOpacity(src->opacity());
//            m_SceneData[k].m_pIItem->setRotation(src->rotation());
//            m_SceneData[k].m_pIItem->setScale(src->scale());
        }
    }

    void undo()
    { addCellList(); }

    void redo()
    { removeCellList(); }

};

class CopyAnimation : public AddRemoveCell
{
public:
CopyAnimation(JistDocument *document, int index, JistTableView *tableView,  QVector< QList < JistCommonData > > *aniData, QList < JistCommonData> *sceneData, JistCommonData *data);
//    CopyAnimation(JistDocument *document, int index, JistTableView *tableView,  QVector< QList < JistCommonData > > *aniData, QList < JistCommonData> *sceneData, JistCommonData *data)
//        : AddRemoveCell(document, index, tableView)
//    {
//        setText(QCoreApplication::translate("Undo Commands", "Add Animation(s)"));
//        if(!m_OwnData)
//            m_OwnData = new JistCommonData();
//        m_OwnData->copyData(*data);
//        m_AniData = *aniData;
//        for(int k = 0; k < aniData->count(); k++){
//            int cellCnt = aniData->at(k).count();
//            for(int j = 0; j < cellCnt; j++){
//                m_AniData[k][j].copyData(aniData->at(k).at(j));
//                aniData->at(k).at(j).m_pIItem->setZValue(index + 1 + k * 0.0001f);
//            }
//        }

//        m_SceneData = *sceneData;
//        for(int k = 0; k < m_SceneData.count(); k++){
//            m_SceneData[k].copyData(sceneData->at(k));
//        }

//    }

    void undo()
    { removeAnimation(); }

    void redo()
    { copyAnimation(); }

};

class RemoveAnimation : public AddRemoveCell
{
public:

    RemoveAnimation(JistDocument *document, int index, JistTableView *tableView,  QVector< QList < JistCommonData > > *aniData, QList < JistCommonData> *sceneData, JistCommonData *data)
        : AddRemoveCell(document, index, tableView)
    {
        setText(QCoreApplication::translate("Undo Commands", "Remove Animation(s)"));
        if(!m_OwnData)
            m_OwnData = new JistCommonData();
        m_OwnData->copyData(*data);
        m_AniData = *aniData;
        for(int k = 0; !m_AniData.isEmpty() && k < aniData->count(); k++){
            int cellCnt = aniData->at(k).count();
            for(int j = 0; j < cellCnt; j++){
                m_AniData[k][j].copyData(aniData->at(k).at(j));
            }
        }
        m_SceneData = *sceneData;
        for(int k = 0; !m_SceneData.isEmpty() && k < m_SceneData.count(); k++){
            m_SceneData[k].copyData(sceneData->at(k));
        }
    }

    void undo()
    { copyAnimation(); }

    void redo()
    { removeAnimation(); }

};

#endif // ADDCELLL_H
